package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.PaymentType;
import com.atguigu.lease.web.admin.service.PaymentTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Tag(name = "支付方式管理")
@RequestMapping("/admin/payment")
@RestController
public class PaymentTypeController {

    @Autowired
    private PaymentTypeService paymentTypeService;


    /**
     * 接口查询。保证createTime updateTime isDeleted 不要返回给前端！ 不要转成json！ 后台的数据判断依据！
     *
     * handler -> 对象 -》 handlerAdapter -> 序列化(jackson : 默认将对象的所有属性全部转成json | 通过注解设置@JsonIgnore) -》 json
     *     对象（id name age）  ->  { id name age }
     *
     *   1. 实体类添加忽略注解 @JsonIgnore -> jackson
     *      查询，不返回 时间+逻辑删除字段  createTime updateTime isDeleted
     *   2. 确保是逻辑删除  @TableLogic isDeleted
     *
     *   3. @JsonIgnore的后续影响,使用mybatis-plus给createTime updateTime自动赋值
     *      步骤1:
     *         @TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE)
     *      步骤2：
     *         创建MetaObjectHandler的实现，指定具体插入的数据值
     *
     * @return
     */
    @Operation(summary = "查询全部支付方式列表")
    @GetMapping("list")
    public Result<List<PaymentType>> listPaymentType() {
        List<PaymentType> paymentTypeList = paymentTypeService.list();
        return Result.ok(paymentTypeList);
    }

    @Operation(summary = "保存或更新支付方式")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdatePaymentType(@RequestBody PaymentType paymentType) {
        boolean b = paymentTypeService.saveOrUpdate(paymentType);
        if (!b){
            return Result.fail();
        }
        return Result.ok();
    }

    @Operation(summary = "根据ID删除支付方式")
    @DeleteMapping("deleteById")
    public Result deletePaymentById(@RequestParam Long id) {
        boolean b = paymentTypeService.removeById(id);
        if (!b){
            return Result.fail();
        }
        return Result.ok();
    }

}















